#!/bin/bash
set -e
echo "本脚本将会安装 Docker 和其他必要的软件包。"
read -p "是否继续？[y/n]: " choice
case ${choice} in
  y|Y )
    echo "开始安装..."
    ;;
  n|N )
    echo "取消安装。"
    exit 0
    ;;
  * )
    echo "无效的选项，取消安装。"
    exit 1
    ;;
esac

echo "配置docker的yum源"
sudo tee /etc/yum.repos.d/docker-ce.repo <<-'EOF'
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF
if [ $? -eq 0 ]; then
echo "配置docker的yum源成功"
else
echo "配置docker的yum源失败"
exit 1
fi
echo "可用的 Docker CE 版本："
yum list docker-ce --showduplicates | sort -r
read -p "请输入要安装的 Docker CE 版本号（如 20.10.9）：" DOCKER_VERSION

echo "第1步：停止Firewalld"
systemctl stop firewalld
if [ $? -eq 0 ]; then
echo "Firewalld已停止"
else
echo "停止Firewalld失败"
exit 1
fi

echo "第1步：停止Firewalld"
systemctl stop firewalld
if [ $? -eq 0 ]; then
echo "Firewalld已停止"
else
echo "停止Firewalld失败"
exit 1
fi

echo "第2步：禁用Firewalld"
systemctl disable firewalld
if [ $? -eq 0 ]; then
echo "Firewalld已禁用"
else
echo "禁用Firewalld失败"
exit 1
fi

echo "第3步：更新SELinux配置"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
if [ $? -eq 0 ]; then
echo "SELinux配置已更新"
else
echo "更新SELinux配置失败"
exit 1
fi

echo "第4步：禁用交换空间"
sudo swapoff -a
if [ $? -eq 0 ]; then
echo "交换空间已禁用"
else
echo "禁用交换空间失败"
exit 1
fi

echo "第5步：永久禁用交换空间"
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
if [ $? -eq 0 ]; then
echo "交换空间已永久禁用"
else
echo "永久禁用交换空间失败"
exit 1
fi

echo "第6步：将主机名设置为Master"
sudo hostnamectl set-hostname master
if [ $? -eq 0 ]; then
echo "主机名已设置为Master"
else
echo "设置主机名失败"
exit 1
fi

echo "第7步：使用Master IP地址更新hosts文件"
sudo sed -i '1s/^.*$/127.0.0.1 master/' /etc/hosts
if [ $? -eq 0 ]; then
echo "hosts文件已更新"
else
echo "更新hosts文件失败"
exit 1
fi

echo "第8步：安装Docker CE ${DOCKER_VERSION}"
sudo yum -y install docker-ce-${DOCKER_VERSION} 
if [ $? -eq 0 ]; then
echo "Docker CE ${DOCKER_VERSION} 已安装"
else
echo "安装Docker CE ${DOCKER_VERSION} 失败"
exit 1
fi

echo "第9步：创建Docker配置目录"
mkdir /etc/docker
if [ $? -eq 0 ]; then
echo "Docker配置目录已创建"
else
echo "创建Docker配置目录失败"
exit 1
fi

echo "第10步：配置Docker守护进程"
cat > /etc/docker/daemon.json << EOF
{
"oom-score-adjust": -1000,
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
if [ $? -eq 0 ]; then
echo "Docker守护进程已配置"
else
echo "配置Docker守护进程失败"
exit 1
fi

echo "第11步：启动Docker服务"
systemctl enable docker
systemctl daemon-reload
systemctl start docker
systemctl restart docker
if [ $? -eq 0 ]; then
echo "Docker服务已启动"
else
echo "启动Docker服务失败"
exit 1
fi

echo "第12  步：安装网络所需的软件包"
yum install -y socat conntrack ebtables ipset glusterfs-client nfs-utils ipvsadm
if [ $? -eq 0 ]; then
echo "网络软件包已安装"
else
echo "安装网络软件包失败"
exit 1
fi

echo "安装完成！"